using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EcoEstadisticas
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RegistradorEnBD register = new RegistradorEnBD();
            try
            {
                MessageBox.Show("PERA A QUE TERMINE");
                register.insertarNbisPor(System.Convert.ToInt16(TB_anio.Text.Trim()), System.Convert.ToInt16(TB_Trimestre.Text.Trim()));
            }
            catch (Exception exep)
            {
                MessageBox.Show(exep.Message);
                return;
            }
            MessageBox.Show("TERMINO");
        }

        private void MostrarNBI_Click(object sender, EventArgs e)
        {
            String nbi_hogar = "SELECT * FROM NBI_HOGAR WHERE ANIO = @ANIO AND TRIMESTRE = @TRIMESTRE";
            SqlCommand todosLosRegistros = new SqlCommand(nbi_hogar,ConeccionBD.getInstance().getConneccion());
            todosLosRegistros.Parameters.Add("@ANIO", SqlDbType.SmallInt);
            todosLosRegistros.Parameters.Add("@TRIMESTRE", SqlDbType.TinyInt);
            try
            {
                todosLosRegistros.Parameters["@ANIO"].Value = (System.Convert.ToInt16(TB_anio.Text.Trim()));
                todosLosRegistros.Parameters["@TRIMESTRE"].Value = (System.Convert.ToInt16(TB_Trimestre.Text.Trim()));
            }
            catch (Exception EXEPTION)
            {
                MessageBox.Show(EXEPTION.Message);
                return;
            }

            SqlDataAdapter adapter = new SqlDataAdapter(todosLosRegistros);
            DataTable tabla = new DataTable();

            adapter.Fill(tabla);

            DG_NBI_HOGAR.DataSource = tabla;
        }

        private void boton_Tabla_Home_Click(object sender, EventArgs e)
        {
            String hogar = "SELECT CODUSU,NRO_HOGAR,II1,IV10 FROM HOME WHERE ANO4=@ANIO AND TRIMESTRE=@TRIMESTRE";
            SqlCommand todosLosRegistros = new SqlCommand(hogar, ConeccionBD.getInstance().getConneccion());
            SqlDataAdapter adapter = new SqlDataAdapter(todosLosRegistros);
            DataTable tabla = new DataTable();

            todosLosRegistros.Parameters.Add("@ANIO",SqlDbType.SmallInt);
            todosLosRegistros.Parameters.Add("@TRIMESTRE",SqlDbType.TinyInt);
            try
            {
                todosLosRegistros.Parameters["@ANIO"].Value = (System.Convert.ToInt16(TB_anio.Text.Trim()));
                todosLosRegistros.Parameters["@TRIMESTRE"].Value = (System.Convert.ToInt16(TB_Trimestre.Text.Trim()));
            }
            catch (Exception e2)
            {
                MessageBox.Show(e2.Message);
                return;
            }

            adapter.Fill(tabla);

            DG_HOME.DataSource = tabla;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            String individuos = "SELECT CODUSU,NRO_HOGAR,CH12,ESTADO,COMPONENTE,CH13,CH14 FROM IND WHERE ANO4=@ANIO AND TRIMESTRE=@TRIMESTRE";
            SqlCommand todosLosRegistros = new SqlCommand(individuos, ConeccionBD.getInstance().getConneccion());
            SqlDataAdapter adapter = new SqlDataAdapter(todosLosRegistros);
            DataTable tabla = new DataTable();

            todosLosRegistros.Parameters.Add("@ANIO", SqlDbType.SmallInt);
            todosLosRegistros.Parameters.Add("@TRIMESTRE", SqlDbType.TinyInt);
            try
            {

                todosLosRegistros.Parameters["@ANIO"].Value = (System.Convert.ToInt16(TB_anio.Text.Trim()));
                todosLosRegistros.Parameters["@TRIMESTRE"].Value = (System.Convert.ToInt16(TB_Trimestre.Text.Trim()));
            }
            catch (Exception e3)
            {
                MessageBox.Show(e3.Message);
                return;
            }


            adapter.Fill(tabla);

            DG_IND.DataSource = tabla;
        }

        private void TB_anio_TextChanged(object sender, EventArgs e)
        {    

        }

        private void boton_Ponderacion_Click(object sender, EventArgs e)
        {
            RegistradorEnBD register = new RegistradorEnBD();
            try
            {
                int anio = (System.Convert.ToInt16(TB_anio.Text.Trim()));
                int trimestre = (System.Convert.ToInt16(TB_Trimestre.Text.Trim()));
                int region = (System.Convert.ToInt16(TB_region.Text.Trim()));
                register.insertarPonderacionNBIindividual(anio, trimestre, region);
            }
            catch (SqlException sqlexp)
            {
                MessageBox.Show(sqlexp.Message);
                return;
            }
            catch (Exception exep)
            {
                MessageBox.Show(exep.Message);
                return;
            }
            
            MessageBox.Show("TERMINADO");
        }

        private void Boton_NBI_DECIFR_Click(object sender, EventArgs e)
        {            
            double[] nbiPonderadorPorDecifr = new double[10];
            PonderadorPorNBI pondera = new PonderadorPorNBI();
            try
            {
                int anio = (System.Convert.ToInt16(TB_anio.Text.Trim()));
                int trimestre = (System.Convert.ToInt16(TB_Trimestre.Text.Trim()));
                int region = (System.Convert.ToInt16(TB_region.Text.Trim()));           
            DataTable tabla = new DataTable();
            tabla = GateWayCustom.getInstance().hogaresConNbi(anio,trimestre,region);
            for (int i = 0; i < 10; i++)                      
            {                              
              nbiPonderadorPorDecifr[i] = pondera.pondera(i, anio, trimestre, tabla);
            }
            GateWayCustom.getInstance().insertarEnPorcionNBI_Decil(anio, trimestre, region, nbiPonderadorPorDecifr);
            MessageBox.Show("TERMINADO");
            }catch (Exception fukingExeption)
            {
                MessageBox.Show(fukingExeption.Message);
                return;
            }
        }
    }
}